package com.ruoyi.erp.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SortUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.common.enums.ProductType;
import com.ruoyi.erp.domain.XzgProduct;
import com.ruoyi.erp.mapper.XzgProductMapper;
import com.ruoyi.erp.service.IXzgProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author qiuwencong
 * @Date 2021/12/31 10:20
 * @Package com.ruoyi.erp.service.impl
 * @Description
 */
@Service
public class XzgProductServiceImpl implements IXzgProductService {

    @Autowired
    private XzgProductMapper productMapper;

    @Override
    public int addProduct(XzgProduct product) {
        if (checkProductCode(product.getProductId(), product.getProductCode())) {
            throw new ServiceException("产品编号已存在");
        }
        if (checkProductName(product.getProductId(), product.getProductName())) {
            throw new ServiceException("产品名称已存在");
        }
        return productMapper.insertSelective(product);
    }

    @Override
    public Boolean checkProductName(Long id, String name) {
        return productMapper.selectSameName(id, name) > 0;
    }

    @Override
    public Boolean checkProductCode(Long id, String code) {
        return productMapper.selectSameCode(id, code) > 0;
    }

    @Override
    public List<XzgProduct> selectProductList(XzgProduct product) {
        SortUtils.sort(product);
        return productMapper.selectProductList(product);
    }

    @Override
    public XzgProduct selectProductById(Long productId) {
        return productMapper.selectByPrimaryKey(productId);
    }

    @Override
    public int updateProduct(XzgProduct product) {
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public int deleteProductById(Long[] productIds, String username) {
        return productMapper.logicDeleteProductById(productIds, username);
    }

    @Override
    public List<XzgProduct> selectByCodeOrName(String name) {
        if (StringUtils.isBlank(name)) {
            return new ArrayList<>();
        }
        return productMapper.selectByCodeOrName(name);
    }

    @Override
    public Integer selectCountById(Long productId) {
        return productMapper.selectCountById(productId);
    }

    @Override
    public Map<String, List<String>> importProducts(List<XzgProduct> products, boolean updateSupport, String username) {
        List<String> insertSuccess = new ArrayList<>();
        List<String> updateSuccess = new ArrayList<>();
        List<String> fail = new ArrayList<>();
        for (XzgProduct product : products) {
            XzgProduct exist = productMapper.selectByCode(product.getProductCode());
            if (Objects.isNull(exist)) {
                product.setUpdateBy(username);
                product.setUpdateTime(new Date());
                product.setCreateBy(username);
                product.setCreateTime(new Date());
                product.setDeleted("0");
                product.setProductType(ProductType.getKeyByValue(product.getProductType()));
                productMapper.insertSelective(product);
                insertSuccess.add(product.getProductCode());
            } else if (updateSupport) {
                exist.setUpdateBy(username);
                exist.setUpdateTime(new Date());
                exist.setFinished(product.getFinished());
                exist.setMaterial(product.getMaterial());
                exist.setProductName(product.getProductName());
                exist.setProductType(ProductType.getKeyByValue(product.getProductType()));
                productMapper.updateByPrimaryKeySelective(exist);
                updateSuccess.add(exist.getProductCode());
            } else {
                fail.add("编号为：" + product.getProductCode() + "的数据已存在");
            }
        }
        Map<String, List<String>> map = new HashMap<>(3);
        map.put("insert", insertSuccess);
        map.put("update", updateSuccess);
        map.put("fail", fail);
        return map;
    }

}
